<?php
/* 
* [***********************]
* @author: mazhenyu[houdunwangmzy@163.com]
* @Date:   2014-06-25 17:09:00
* @Last Modified time: 2014-06-29 22:11:59
*/
class HtmlControl extends CommonControl{
	public function __auto(){
		//检测用户是否登录
		if(!isset($_SESSION['adminname']) || !isset($_SESSION['aid'])){
			go(U('Admin/Login/index'));
		}
	}
	/**
	 * [index 生成首页静态]
	 * @return [type] [description]
	 */
	public function index(){
		if($this->_create_html('Index/index', ROOT_PATH . 'index.html')){
			$this->success('生成首页静态成功',U('Admin/Index/welcome'));
		}
		$this->error('生成首页静态失败',U('Admin/Index/welcome'));
	}

	/**
	 * [list_ 生成列表页静态]
	 * @return [type] [description]
	 */
	public function list_(){
		$cate = K('Category')->get_all();
		foreach ($cate as $v) {
			//如果不是生成静态，则略过
			if(!$v['is_listhtml']) continue;
			$_GET['cid'] = $v['cid'];
			$_GET['page'] = 1;

			//生成静态
			if(!$this->_create_html('List/index',STATIC_PATH . $v['htmldir'] . '/index.html')){
				header('Content-type:text/html;charset=utf-8');
				die('生成列表页静态失败');
			}

			for ($i=1; $i <= Page::$staticTotalPage; $i++) { 
				$_GET['page'] = $i;
				$this->_create_html('List/index',STATIC_PATH . $v['htmldir'] . '/' . $i .'.html');
			}
			

		}
		
		$this->success('生成列表页静态成功',U('Admin/Index/welcome'));

	}

	/**
	 * [details 生成内容页静态]
	 * @return [type] [description]
	 */
	public function details(){
		$model = K('ArticleCateView');
		//统计总数
		$total = $model->get_join_count('category');
		$pageObj = new Page($total,10);

		//得出文章与栏目关联信息
		$field = 'aid,is_archtml,htmldir';
		$data = $model->get_join_data('category',$field,$pageObj->limit());

		foreach ($data as $v) {
			//如果不是生成静态，则略过
			if(!$v['is_archtml']) continue;
			$_GET['aid'] = $v['aid'];
			//生成静态
			if(!$this->_create_html('Details/index',STATIC_PATH . $v['htmldir'] . '/arc/'.$v['aid'].'.html')){
				header('Content-type:text/html;charset=utf-8');
				die('生成内容页静态失败');
			}
		}
		//如果到了最后一页，则生成完成
		if($pageObj->selfPage == $pageObj->totalPage){
			$this->success('生成内容页完成',U('Admin/Index/welcome'));
		}else{
			$this->success('生成第'.$pageObj->selfPage.'页完成',U('details',array('page'=>$pageObj->selfPage+1)));
		}



		
	}


	private function _create_html($CM, $file){
		ob_start();
		A('Index/' . $CM);
		$content = ob_get_contents();
		ob_clean();

		//获得路径
		$dir = dirname($file);
		is_dir($dir) || mkdir($dir,0777,true);

		if(file_put_contents($file, $content)){
			return true;
		}
		return false;
	}
}

